/****************************************************
          File Name:34-7.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Mon 01 Sep 2014 03:24:53 PM CST
*****************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<signal.h>

void dealing(int sig)
{
    switch(sig)
    {
        case SIGTTIN:
            fprintf(stderr,"%s\n","caught SIGTTIN");
            signal(sig,SIG_DFL);
            break;
        case SIGTTOU:
            fprintf(stderr,"%s\b","caught SIGTOUT");
            signal(sig,SIG_DFL);
            break;
        case SIGTSTP:
            fprintf(stderr,"%s\n","caught SIGTSTP");
            signal(sig,SIG_DFL);
            break;
    }

    signal(sig,SIG_DFL);
    raise(sig);
}

int main(int argc , char *argv[])
{
    struct sigaction act;

    sigemptyset(&act.sa_mask);
    act.sa_flags=SA_RESTART;
    act.sa_handler=dealing;

    if(sigaction(SIGTTOU,&act,NULL)==-1);
    if(sigaction(SIGTTIN,&act,NULL)==-1);
    if(sigaction(SIGTSTP,&act,NULL)==-1)
    {
        perror("sigaction");
        exit(1);
    }

    switch(fork())
    {
        case -1:
            perror("fork");
            exit(1);
        case 0:
            for(;;)
                pause();
        default:
            break;
    }
    alarm(30);
    printf("the end of parent\n");
    exit(0);
}
